package com.gexiaobao.pigeon.ui.fragment.mine.device

import android.os.Bundle
import android.view.View
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.showMessage
import com.gexiaobao.pigeon.app.model.bean.DeviceInfoResponse
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.databinding.FragmentDeviceDetailBinding
import com.gexiaobao.pigeon.ui.dialog.RxDialogDefault
import com.gexiaobao.pigeon.viewmodel.MyDeviceViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.parseState
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 17:40
 * @Describe    : 设备详情
 */
class FragmentDeviceDetail : BaseFragment<MyDeviceViewModel, FragmentDeviceDetailBinding>() {

    private var mId: Int = 0
    private var mImsi: String = ""

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        mDatabind.includeBar.toolbar.initClose("设备详情") {
            nav().navigateUp()
        }
    }

    override fun initData() {
        super.initData()
        mId = arguments?.getInt("id")!!
        mImsi = requireArguments().getString("imsi").toString()

        mDatabind.swipeRefreshLayout.init {
            showLoading(getString(R.string.loading))
            mViewModel.getPigeonDeviceInfo(mId)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.tvDeviceDetailModify, mDatabind.tvDeviceDetailDelete) {
            when (it) {
                mDatabind.tvDeviceDetailModify -> {
                    nav().navigateAction(R.id.action_to_add_device, Bundle().apply {
                        putString("imsi", mImsi)
                        putString("title", "修改设备")
                        putInt("id", mId)
                    })
                }

                mDatabind.tvDeviceDetailDelete -> {
                    deleteDevice()
                }
            }
        }
    }

    private fun deleteDevice() {
        val dialog = RxDialogDefault(context)
        dialog.setTitle("是否删除设备")
        dialog.setSureListener {
            showLoading(getString(R.string.loading))
            mViewModel.deletePigeonDevice(mId)
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        showLoading(getString(R.string.loading))
        mViewModel.getPigeonDeviceInfo(mId)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.deviceDetailInfo.observe(viewLifecycleOwner) { result ->
            dismissLoading()
            if (mDatabind.swipeRefreshLayout.isRefreshing) {
                mDatabind.swipeRefreshLayout.isRefreshing = false
            }
            parseState(result, {
                showDataUI(it)
            }, {
                showMessage(it.msg)
                if (it.code == 201103) {
                    mViewModel.deviceScanStatus.set("鸽钟网络异常")//扫描状态
                    mDatabind.ivDetailDeviceScanStatus.setBackgroundResource(R.mipmap.ic_lightbulb_outline_deep_orange_300_24dp)
                }
                mViewModel.deviceCode.set(mImsi)
            })
        }

        mViewModel.deletePigeonDevice.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                RxToast.showToast("删除成功")
                nav().navigateUp()
            } else {
                RxToast.showToast(it.errorMsg)
            }
        }
    }

    private fun showDataUI(it: DeviceInfoResponse) {
        mImsi = it.imsi
        mViewModel.deviceType.set("电子扫描鸽钟")
        mViewModel.deviceCode.set(it.imsi)
        mViewModel.deviceLongitude.set(it.lon)
        mViewModel.deviceLatitude.set(it.lat)
        var scanStatus = ""
        val normalDrawableId = R.mipmap.ic_lightbulb_outline_green_300_24dp
        val errorDrawableId = R.mipmap.ic_lightbulb_outline_deep_orange_300_24dp
        var scanStatusDrawId = 0
        when (it.currentState) {
            0 -> {
                scanStatus = "每日扫描"
                scanStatusDrawId = normalDrawableId
            }

            4 -> {
                scanStatus = "比赛中"
                scanStatusDrawId = normalDrawableId
            }

            7 -> {
                scanStatus = "家庭训放"
                scanStatusDrawId = normalDrawableId
            }

            else -> {
                scanStatus = "鸽钟网络异常"
                scanStatusDrawId = errorDrawableId
            }
        }
        mDatabind.ivDetailDeviceScanStatus.setBackgroundResource(scanStatusDrawId)
        mViewModel.deviceScanStatus.set(scanStatus)//扫描状态

        var contactStatus = ""
        val noContactStatusDrawableId = R.mipmap.ic_signal_wifi_off_deep_orange_300_24dp
        val weekContactStatusDrawableId = R.mipmap.ic_signal_wifi_1_bar_green_300_24dp
        val inContactStatusDrawableId = R.mipmap.ic_signal_wifi_2_bar_green_300_24dp
        val betterContactStatusDrawableId = R.mipmap.ic_signal_wifi_4_bar_green_300_24dp
        var contactStatusDrawId = 0
        when (it.contactStatus) {
            1 -> {
                contactStatus = "无信号"
                contactStatusDrawId = noContactStatusDrawableId
            }

            2 -> {
                contactStatus = "信号弱"
                contactStatusDrawId = weekContactStatusDrawableId
            }

            3 -> {
                contactStatus = "信号中"
                contactStatusDrawId = inContactStatusDrawableId
            }

            4 -> {
                contactStatus = "信号强"
                contactStatusDrawId = betterContactStatusDrawableId
            }
        }
        mDatabind.ivDetailDeviceCommStatus.setBackgroundResource(contactStatusDrawId)
        mViewModel.deviceCommStatus.set(contactStatus)


        var tBStatus = ""
        var tBStatusDrawId = 0
        when (it.boardOneStatus) {
            0 -> {
                tBStatus = "未连接"
                tBStatusDrawId = errorDrawableId
                mDatabind.ivDetailDeviceTbStatus.visibility = View.VISIBLE
            }

            1 -> {
                tBStatus = "正常"
                tBStatusDrawId = normalDrawableId
                mDatabind.ivDetailDeviceTbStatus.visibility = View.VISIBLE
            }

            else -> {
                mDatabind.ivDetailDeviceTbStatus.visibility = View.GONE
            }
        }
        mDatabind.ivDetailDeviceTbStatus.setBackgroundResource(tBStatusDrawId)
        mViewModel.deviceTBStatus.set(tBStatus)//踏板1状态

        var tBStatus1 = ""
        var tBStatusDrawId1 = 0
        when (it.boardTwoStatus) {
            0 -> {
                tBStatus1 = "未连接"
                tBStatusDrawId1 = errorDrawableId
                mDatabind.ivDetailDeviceTbStatus1.visibility = View.VISIBLE
            }

            1 -> {
                tBStatus1 = "正常"
                tBStatusDrawId1 = normalDrawableId
                mDatabind.ivDetailDeviceTbStatus1.visibility = View.VISIBLE
            }

            else -> {
                mDatabind.ivDetailDeviceTbStatus1.visibility = View.GONE
            }
        }
        mDatabind.ivDetailDeviceTbStatus1.setBackgroundResource(tBStatusDrawId1)
        mViewModel.deviceTBStatus1.set(tBStatus1)//踏板2状态

        var tBStatus2 = ""
        var tBStatusDrawId2 = 0
        when (it.boardThreeStatus) {
            0 -> {
                tBStatus2 = "未连接"
                tBStatusDrawId2 = errorDrawableId
                mDatabind.ivDetailDeviceTbStatus2.visibility = View.VISIBLE
            }

            1 -> {
                tBStatus2 = "正常"
                tBStatusDrawId2 = normalDrawableId
                mDatabind.ivDetailDeviceTbStatus2.visibility = View.VISIBLE
            }

            else -> {
                mDatabind.ivDetailDeviceTbStatus2.visibility = View.GONE
            }
        }
        mDatabind.ivDetailDeviceTbStatus2.setBackgroundResource(tBStatusDrawId2)
        mViewModel.deviceTBStatus2.set(tBStatus2)//踏板3状态

        var gPSStatus = ""
        var gPSDrawId = 0
        when (it.gpsStatus) {
            1 -> {
                gPSStatus = "正常"
                gPSDrawId = normalDrawableId
                mDatabind.ivDetailDeviceGpsStatus.visibility = View.VISIBLE
            }

            0 -> {
                gPSStatus = "异常"
                gPSDrawId = errorDrawableId
                mDatabind.ivDetailDeviceGpsStatus.visibility = View.VISIBLE
            }

            else -> {
                mDatabind.ivDetailDeviceGpsStatus.visibility = View.GONE
            }
        }
        mDatabind.ivDetailDeviceGpsStatus.setBackgroundResource(gPSDrawId)
        mViewModel.deviceGPSStatus.set(gPSStatus)//gps状态

        var upsStatus = ""
        var upsDrawId = 0
        when (it.ups) {
            0 -> {
                upsStatus = "异常"
                upsDrawId = errorDrawableId
                mDatabind.ivDetailDevicePowerStatus.visibility = View.VISIBLE
            }

            1 -> {
                upsStatus = "正常"
                upsDrawId = normalDrawableId
                mDatabind.ivDetailDevicePowerStatus.visibility = View.VISIBLE
            }

            else -> {
                mDatabind.ivDetailDevicePowerStatus.visibility = View.GONE
            }
        }
        mDatabind.ivDetailDevicePowerStatus.setBackgroundResource(upsDrawId)
        mViewModel.devicePowerStatus.set(upsStatus)//备用电源

        var cameraStatus = ""
        var cameraDrawId = 0
        when (it.cameraStatus) {
            0 -> {
                cameraStatus = "异常"
                cameraDrawId = errorDrawableId
                mDatabind.ivDetailDeviceCameraStatus.visibility = View.VISIBLE
            }

            1 -> {
                cameraStatus = "正常"
                cameraDrawId = normalDrawableId
                mDatabind.ivDetailDeviceCameraStatus.visibility = View.VISIBLE
            }

            else -> {
                mDatabind.ivDetailDeviceCameraStatus.visibility = View.GONE
            }
        }
        mDatabind.ivDetailDeviceCameraStatus.setBackgroundResource(cameraDrawId)
        mViewModel.deviceCameraStatus.set(cameraStatus)//相机状态

        mViewModel.deviceDays.set(it.daysRemaining.toString() + "天")
    }
}